package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.dao.SpecialBannerDao;
import com.ytjj.qmyx.admin.model.request.SpecialBannerRequest;
import com.ytjj.qmyx.admin.model.response.SpecialBannerResponse;
import com.ytjj.qmyx.admin.model.response.SpecialResponse;
import com.ytjj.qmyx.admin.service.SpecialBannerService;
import com.ytjj.qmyx.mall.mapper.SubjectsMapper;
import com.ytjj.qmyx.mall.model.SpecialBanner;
import com.ytjj.qmyx.mall.model.Subjects;
import com.ytjj.qmyx.mall.model.SubjectsExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class SpecialBannerServiceImpl implements SpecialBannerService {

	@Resource
	private SpecialBannerDao specialBannerDao;
	@Resource
	private SubjectsMapper subjectsMapper;

	@Override
	public List<SpecialResponse> getSpecialList() {
		List<SpecialResponse> responses = specialBannerDao.getSpecialList();
		List<SpecialResponse> results = new LinkedList<>();
		responses.stream().forEach(item -> {
			if ("mall".equals(item.getType())) {
				item.setTitle(item.getTitle() + "-1");
				results.add(item);
			}else {
				results.add(item);
			}
		});
		return results;
	}

	@Override
	public List<SpecialResponse> getSpecialDynamicList() {
		List<SpecialResponse> specialDynamicList = specialBannerDao.getSpecialDynamicList();
		for (SpecialResponse specialResponse : specialDynamicList) {
			if ("dynamic".equals(specialResponse.getType())){
				String title = specialResponse.getTitle()+"-1";
				specialResponse.setTitle(title);
			}
		}
		return specialDynamicList;
	}

	@Override
	public CommonPage<SpecialBannerResponse> getSpeBannerList(SpecialBannerRequest request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<SpecialBannerResponse> speBannerList = specialBannerDao.getSpeBannerList(request.getSpecialId(),request.getSubjectId());
		if (!CollectionUtils.isEmpty(speBannerList)) {
			for (SpecialBannerResponse res : speBannerList) {
				if (null != res.getSubjectId()){
					Subjects subjects = subjectsMapper.selectByPrimaryKey(res.getSubjectId());
					res.setSubjectTitle(subjects.getTitle());
//					res.setType(2);
				}else {
//					res.setType(1);
				}
				if (null == res.getSubjectId() && null != res.getSpecialId()) {
					if (res.getSpecialId() == 0) {
						res.setSpecialTitle("推荐");
					}
				}
			}
		}
		return CommonPage.restPage(speBannerList);
	}

	@Override
	public Integer insertSpeBanner(SpecialBanner specialBanner) {
		return specialBannerDao.insertSpeBanner(specialBanner);
	}

	@Override
	public Integer updateSpeBanner(SpecialBanner specialBanner) {
		return specialBannerDao.updateSpeBanner(specialBanner);
	}

	@Override
	public Integer deleteSpeBanner(Integer id) {
		return specialBannerDao.deleteSpeBanner(id);
	}

	@Override
	public CommonResult getSubjectList() {
		SubjectsExample subjectsExample = new SubjectsExample();
		subjectsExample.createCriteria().andIsDeletedEqualTo(0);
		List<Subjects> subjects = subjectsMapper.selectByExample(subjectsExample);
		return CommonResult.success(subjects);
	}
}
